package www.ld.com.domain;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class R<T> {
    @ApiModelProperty("状态")
    private CodeType type;
    @ApiModelProperty("消息")
    private String message;
    @ApiModelProperty(value = "返回对象")
    private T Data;

    public static R ok() {
        return ok(null);
    }

    public static <T> R ok(T data) {
        return new R(CodeType.SUCCESS, CodeType.SUCCESS.text, data);
    }

    public static R error() {
        return error(CodeType.ERROR.text, null);
    }

    public static R error(String msg) {
        return error(msg, null);
    }

    public static <T> R error(T data) {
        return error(CodeType.ERROR.text, data);
    }

    public static <T> R error(String msg, T data) {
        return new R(CodeType.ERROR, msg, data);
    }


    public static R expire() {
        return expire(CodeType.EXPIRE.text, null);
    }

    public static R expire(String msg) {
        return expire(msg, null);
    }

    public static <T> R expire(T data) {
        return expire(CodeType.EXPIRE.text, data);
    }

    public static <T> R expire(String msg, T data) {
        return new R(CodeType.EXPIRE, msg, data);
    }

    public static R unauthorized() {
        return unauthorized(CodeType.UNAUTHORIZED.text, null);
    }
    public static <T> R unauthorized(String msg,T date) {
        return new R(CodeType.UNAUTHORIZED,msg,date);
    }
    @AllArgsConstructor
    private enum CodeType {
        SUCCESS("成功"), ERROR("失败"),
        EXPIRE("过期"), UNAUTHORIZED("未授权");
        private String text;
    }

}
